package com.canbot.u05.activity.shoppingGuide.advisory;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.activity.ChoosePicActivity;
import com.canbot.u05.bean.AdvisoryQA;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.PathConst;
import com.canbot.u05.utils.ToastTools;

import java.util.UUID;


/**
 * 咨询问答--编辑回复语界面
 */
public class EditActivity extends BaseActivityNoTitle implements View.OnClickListener {

        private final static String TAG = "EditActivity";

        private final String AUDIO_SALE_DIR = PathConst.SHOPPING_GUIDE_DIR + "advisory/";

        private final static int RESULT_CODE_PIC = 101;

        private Button btnBack;

        private TextView tvPic, tvAudio;

        private TextView tvTtile;

        private AdvisoryQA advisoryQA;

        private boolean isSuccess;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_edit);
                isSuccess = MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, AUDIO_SALE_DIR + "hdszyd_zxwd_u05_001.wav");
                if (!isSuccess) {
                        ToastTools.newInstance(this).showText(getString(R.string.network_error));
                }
                initData();
                setViews();


        }

        /**
         * 初始化数据
         */
        private void initData() {
                advisoryQA = new AdvisoryQA();
                String data = getIntent().getStringExtra("question");
                String uuid = UUID.randomUUID().toString().replace("-", "");
                advisoryQA.setId(uuid);
                advisoryQA.setQuestion(data);
                Logger.e(TAG, "initData()= " + data);

        }

        private void setViews() {
                btnBack = (Button) findViewById(R.id.btn_back);
                tvPic = (TextView) findViewById(R.id.tv_add_pic);
                tvAudio = (TextView) findViewById(R.id.tv_add_voice);
                tvTtile = (TextView)findViewById(R.id.tv_title) ;

                btnBack.setOnClickListener(this);
                tvAudio.setOnClickListener(this);
                tvPic.setOnClickListener(this);
                tvTtile.setText(advisoryQA.getQuestion());
        }

        @Override
        public void onClick(View view) {

                switch (view.getId()) {
                        case R.id.btn_back:
                                //返回
                                finish();
                                overridePendingTransition(R.anim.face_zoom_enter, R.anim.face_zoom_exit);
                                break;

                        case R.id.tv_add_pic:
                                //选择图片
                                Intent intent = new Intent(this, ChoosePicActivity.class);
                                intent.putExtra("pic", advisoryQA.getPic());
                                Logger.e(TAG, "点击选择图片 pic == " + advisoryQA.getPic());
                                startActivityForResult(intent, RESULT_CODE_PIC);
                                overridePendingTransition(R.anim.face_zoom_enter, R.anim.face_zoom_exit);
                                break;
                        case R.id.tv_add_voice:
                                if (TextUtils.isEmpty(advisoryQA.getPic())){
                                        ToastTools.newInstance(EditActivity.this).showText(getString(R.string.please_select_picture));
                                        return;
                                }

                                //选择音频，并传入data
                                Intent intent2 = new Intent(this, SelectAnswerTypeActivity.class);
                                String js = JSON.toJSONString(advisoryQA);
                                intent2.putExtra("data", js);
                                intent2.putExtra("path", "/uusound");
                                startActivity(intent2);
                                finish();
                                overridePendingTransition(R.anim.face_zoom_enter, R.anim.face_zoom_exit);

                                break;
                }

        }


        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (resultCode == RESULT_OK && data != null) {
                        if (requestCode == RESULT_CODE_PIC) {
                                Bundle path = data.getBundleExtra("path");
                                if (path != null) {
                                        String pic = path.getString("pic");
                                        advisoryQA.setPic(pic);
                                        tvPic.setSelected(true);
                                        Logger.e(TAG, "返回来的数据是 pic " + pic);
                                }
                        }
                }
        }
}
